using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity.Spatial;
using Trust.Casas.Model.Entities;
using Se.Framework.Web.Mvc.GenericCrud;
using Se.Framework.Web.Mvc.GenericCrud.Attributes;

namespace Trust.Casas.Web.Areas.Admin.Models
{
    [GenericCrudTableData("Regiones", typeof(Region), "Id", MetadataType = typeof(RegionMetadata))]
    public class RegionMetadata
    {
        [GenericCrudColumn(RenderMode = GenericCrudRenderMode.None)]
        public int Id { get; set; }

        [Required]
        [Display(Name = "Nombre")]
        public string Name { get; set; }

        [Display(Name = "Location")]
        [GenericCrudColumn(Sortable = false)]
        [TypeConverter(typeof(DbGeometryConverter))]
        public DbGeometry Location { get; set; }

        [Display(Name = "Region")]
        [GenericCrudColumn(RenderMode = GenericCrudRenderMode.DisplayModeOnly, SearchType = typeof(Region), SearchKey = "ParentRegionId", Sortable = true)]
        public Region ParentRegion { get; set; }

        [Display(Name = "Region")]
        [GenericCrudColumn(RenderMode = GenericCrudRenderMode.EditModeOnly, SearchType = typeof(Region))]
        public int? ParentRegionId { get; set; }

        [Display(Name = "Last Update")]
        [GenericCrudColumn(RenderMode = GenericCrudRenderMode.DisplayModeOnly)]
        public DateTime? UpdatedStamp { get; set; }
    }

    public class DbGeometryConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return base.CanConvertFrom(context, sourceType);
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return base.CanConvertTo(context, destinationType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            return base.ConvertFrom(context, culture, value);
        }

        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            return base.ConvertTo(context, culture, value, destinationType);
        }

        public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
        {
            return base.GetCreateInstanceSupported(context);
        }
    }
}